package com.baizhou.honglv.controller;

import com.baizhou.honglv.bean.redis.RedisRepository;
import com.baizhou.honglv.bean.wrapper.ResultWrapper;
import com.baizhou.honglv.dto.LoginSuccessDto;
import com.baizhou.honglv.dto.RegisterSuccessDto;
import com.baizhou.honglv.form.ChangeForm;
import com.baizhou.honglv.form.LoginForm;
import com.baizhou.honglv.form.VerificationForm;
import com.baizhou.honglv.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanghaipeng
 * @since 2021-07-14
 */
@Api(value = "UserController", tags = {"用户API"})
@Controller
@RequestMapping("/yvnhan/user")
public class UserController extends BaseController{

    @Autowired
    UserService userService;

    @Autowired
    RedisRepository redisRepository;

    @ResponseBody
    @ApiOperation(value = "用户登录",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "PhoneNumber", value = "用户手机号"),
            @ApiImplicitParam(name = "password", value = "用户密码")
    })
    @PostMapping("/login")                                      //BindingResult 是一种校验方式
    public ResultWrapper login(@Valid @RequestBody LoginForm loginForm,BindingResult bindingResult){

        validateParams(bindingResult);
        LoginSuccessDto loginSuccessDto = userService.login(loginForm);
        String userId = redisRepository.selectAccessToken(loginSuccessDto.getAccessToken());  //通过token查到对应userid
        if (userId == null || userId.equals("")) {
            return ResultWrapper.failure("登录失败");
        }

        return ResultWrapper.successWithData(loginSuccessDto);

}

    @ResponseBody
    @RequestMapping(path = "/change", method = RequestMethod. POST)
    public ResultWrapper changePassword(@Valid @RequestBody ChangeForm changeForm,
                                        BindingResult bindingResult,
                                        @RequestHeader("ANSWER_ACCESS_TOKEN") String token) {
        validateParams (bindingResult);
        String userId = redisRepository.selectAccessToken (token);
        if (userId==null||userId.equals ("")){
            userId = redisRepository.selectAccessToken2 (token);
        }
        if (userId==null||userId.equals ("")){
            return ResultWrapper.failure ("获取用户失败");
        }
        changeForm.setUserId (userId);
        userService.changePassword (changeForm);
        return ResultWrapper.success("请求成功");
    }

    @ResponseBody
    @ApiOperation(value = "用户注册",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "PhoneNumber", value = "用户手机号"),
            @ApiImplicitParam(name = "note", value = "验证码")
    })
    @PostMapping("/register")                                      //BindingResult 是一种校验方式
    public ResultWrapper register(@Valid @RequestBody VerificationForm verificationForm, BindingResult bindingResult){
//            参数校验
        validateParams(bindingResult);
        RegisterSuccessDto registerSuccessDto = userService.register (verificationForm);
        String userId = redisRepository.selectRegisterAccessToken (registerSuccessDto.getAccessToken());  //通过token查到对应userid
        if (userId == null || userId.equals("")) {
            return ResultWrapper.failure("注册失败");
        }
        return ResultWrapper.successWithData(registerSuccessDto);
    }

    @ResponseBody
    @ApiOperation(value = "用户退出",notes = "")
    @RequestMapping(path = "/exit", method = RequestMethod. POST)
    public ResultWrapper exit(@RequestHeader("ANSWER_ACCESS_TOKEN") String token){
        String userId = redisRepository.selectAccessToken (token);
        String loginAccessToken = redisRepository.selectLoginAccessToken (userId);
        ResultWrapper resultWrapper = new ResultWrapper ();
        if (loginAccessToken!=null) {
            userService.deleteToken (userId);
            resultWrapper.setCode (200);
            resultWrapper.setMessage ("退出登录成功");
        }
        return resultWrapper;
    }

    @ResponseBody
    @ApiOperation(value = "验证码验证",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "PhoneNumber", value = "用户手机号"),
            @ApiImplicitParam(name = "note", value = "验证码")
    })
    @RequestMapping(path = "/verification", method = RequestMethod. POST)
    public ResultWrapper TextVerification(@Valid @RequestBody VerificationForm verificationForm, BindingResult bindingResult){
        validateParams (bindingResult);
        boolean verification = userService.verification (verificationForm);

        ResultWrapper resultWrapper = new ResultWrapper ();

        if (verification){
            resultWrapper.setMessage ("验证码正确");
            resultWrapper.setCode (200);
        }

        return resultWrapper;
    }
}

